package com.rr.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author zhuangcanrun
 * @Date 2022/10/07/11:32
 * @description
 **/

@Slf4j
@Component
public class Demo1ListenerTest {
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "q_rr_test_queue", durable = "true"),
                    exchange = @Exchange(value = "e_rr_test_exchange", type = "topic"),
                    key = "r_rr_test_routine_key"
            ), concurrency = "1"
    )
    public void onMessage(Message message, Channel channel) throws IOException {
        try {
            String messages = new String(message.getBody(), StandardCharsets.UTF_8);
            System.out.println(messages);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
        }
    }
}
